package com.cbs.android.component.application.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;

import com.cbs.android.component.application.R;
import com.cbs.android.component.application.activity.delayedtask.DelayedTask;
import com.cbs.android.component.application.activity.delayedtask.DelayedTaskHandler;
import com.cbs.android.component.application.activity.handler.CBSActivityResultHandler;
import com.cbs.android.component.application.activity.handler.CBSFragmentLifeCyclerHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by broche on 10/31/15.
 */
public class CBSFragment extends Fragment {
    protected List<CBSActivityResultHandler> activityResultHandlers = new ArrayList<>();

    protected List<CBSFragmentLifeCyclerHandler> resumeHandlers = new ArrayList<>();

    protected List<CBSFragmentLifeCyclerHandler> startHandlers = new ArrayList<>();

    protected List<CBSFragmentLifeCyclerHandler> pauseHandlers = new ArrayList<>();

    protected List<CBSFragmentLifeCyclerHandler> stopHandlers = new ArrayList<>();

    protected List<CBSFragmentLifeCyclerHandler> destroyHandlers = new ArrayList<>();

    protected static List<CBSActivityResultHandler> globalActivityResultHandlers = new ArrayList<>();

    protected static List<CBSFragmentLifeCyclerHandler> globalResumeHandlers = new ArrayList<>();

    protected static List<CBSFragmentLifeCyclerHandler> globalStartHandlers = new ArrayList<>();

    protected static List<CBSFragmentLifeCyclerHandler> globalPauseHandlers = new ArrayList<>();

    protected static List<CBSFragmentLifeCyclerHandler> globalStopHandlers = new ArrayList<>();

    protected static List<CBSFragmentLifeCyclerHandler> globalDestroyHandlers = new ArrayList<>();

    private final DelayedTaskHandler delayedHandler = new DelayedTaskHandler();

    protected int getDefaultStartInAnim() {
        return R.anim.cbs_slide_in_right;
    }

    protected int getDefaultStartOutAnim() {
        return R.anim.cbs_slide_out_left;
    }

    protected int getDefaultFinishInAnim() {
        return R.anim.cbs_slide_in_left;
    }

    protected int getDefaultFinishOutAnim() {
        return R.anim.cbs_slide_out_right;
    }

    public CBSActivity getCBSActivity() {
        return (CBSActivity) getActivity();
    }

    @Override
    public void startActivity(Intent intent) {
        startActivity(intent, getDefaultStartInAnim(), getDefaultStartOutAnim());
    }

    public void startActivity(Intent intent, int animin, int animout) {
        super.startActivity(intent);
        getCBSActivity().overridePendingTransition(animin, animout);
    }

    public void startActivityForResult(Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode, getDefaultStartInAnim(), getDefaultStartOutAnim());
    }

    public void startActivityForResult(Intent intent, int requestCode, int animin, int animout) {
        super.startActivityForResult(intent, requestCode);
        getCBSActivity().overridePendingTransition(animin, animout);
    }

    public void doDelayed(DelayedTask delayedTask, Bundle data, long delayedMS) {
        Message message = delayedHandler.obtainMessage(0, delayedTask);
        message.setData(data);
        delayedHandler.sendMessageDelayed(message, delayedMS);
    }


    public void addResumeHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!resumeHandlers.contains(handler)) {
            resumeHandlers.add(handler);
        }
    }

    public void removeResumeHandler(CBSFragmentLifeCyclerHandler handler) {
        if (resumeHandlers.contains(handler)) {
            resumeHandlers.remove(handler);
        }
    }

    public static void addGlobalResumeHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!globalResumeHandlers.contains(handler)) {
            globalResumeHandlers.add(handler);
        }
    }

    public static void removeGlobalResumeHandler(CBSFragmentLifeCyclerHandler handler) {
        if (globalResumeHandlers.contains(handler)) {
            globalResumeHandlers.remove(handler);
        }
    }

    public void onResume() {
        super.onResume();
        for (CBSFragmentLifeCyclerHandler resumeHandler : resumeHandlers) {
            resumeHandler.handler(this);
        }
        for (CBSFragmentLifeCyclerHandler resumeHandler : globalResumeHandlers) {
            resumeHandler.handler(this);
        }
    }

    public void addStartHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!startHandlers.contains(handler)) {
            startHandlers.add(handler);
        }
    }

    public void removeStartHandler(CBSFragmentLifeCyclerHandler handler) {
        if (startHandlers.contains(handler)) {
            startHandlers.remove(handler);
        }
    }

    public static void addGlobalStartHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!globalStartHandlers.contains(handler)) {
            globalStartHandlers.add(handler);
        }
    }

    public static void removeGlobalStartHandler(CBSFragmentLifeCyclerHandler handler) {
        if (globalStartHandlers.contains(handler)) {
            globalStartHandlers.remove(handler);
        }
    }

    public void onStart() {
        super.onStart();
        for (CBSFragmentLifeCyclerHandler startHandler : startHandlers) {
            startHandler.handler(this);
        }
        for (CBSFragmentLifeCyclerHandler startHandler : globalStartHandlers) {
            startHandler.handler(this);
        }
    }

    public void addPauseHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!pauseHandlers.contains(handler)) {
            pauseHandlers.add(handler);
        }
    }

    public void removePauseHandler(CBSFragmentLifeCyclerHandler handler) {
        if (pauseHandlers.contains(handler)) {
            pauseHandlers.remove(handler);
        }
    }

    public static void addGlobalPauseHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!globalPauseHandlers.contains(handler)) {
            globalPauseHandlers.add(handler);
        }
    }

    public static void removeGlobalPauseHandler(CBSFragmentLifeCyclerHandler handler) {
        if (globalPauseHandlers.contains(handler)) {
            globalPauseHandlers.remove(handler);
        }
    }

    public void onPause() {
        super.onPause();
        for (CBSFragmentLifeCyclerHandler pauseHandler : pauseHandlers) {
            pauseHandler.handler(this);
        }
        for (CBSFragmentLifeCyclerHandler pauseHandler : globalPauseHandlers) {
            pauseHandler.handler(this);
        }
    }

    public void addStopHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!stopHandlers.contains(handler)) {
            stopHandlers.add(handler);
        }
    }

    public void removeStopHandler(CBSFragmentLifeCyclerHandler handler) {
        if (stopHandlers.contains(handler)) {
            stopHandlers.remove(handler);
        }
    }

    public static void addGlobalStopHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!globalStopHandlers.contains(handler)) {
            globalStopHandlers.add(handler);
        }
    }

    public static void removeGlobalStopHandler(CBSFragmentLifeCyclerHandler handler) {
        if (globalStopHandlers.contains(handler)) {
            globalStopHandlers.remove(handler);
        }
    }

    public void onStop() {
        super.onStop();
        for (CBSFragmentLifeCyclerHandler stopHandler : stopHandlers) {
            stopHandler.handler(this);
        }
        for (CBSFragmentLifeCyclerHandler stopHandler : globalStopHandlers) {
            stopHandler.handler(this);
        }
    }

    public void addDestroyHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!destroyHandlers.contains(handler)) {
            destroyHandlers.add(handler);
        }
    }

    public void removeDestroyHandler(CBSFragmentLifeCyclerHandler handler) {
        if (destroyHandlers.contains(handler)) {
            destroyHandlers.remove(handler);
        }
    }

    public static void addGlobalDestroyHandler(CBSFragmentLifeCyclerHandler handler) {
        if (!globalDestroyHandlers.contains(handler)) {
            globalDestroyHandlers.add(handler);
        }
    }

    public static void removeGlobalDestroyHandler(CBSFragmentLifeCyclerHandler handler) {
        if (globalDestroyHandlers.contains(handler)) {
            globalDestroyHandlers.remove(handler);
        }
    }

    public void onDestroy() {
        super.onDestroy();
        for (CBSFragmentLifeCyclerHandler destroyHandler : destroyHandlers) {
            destroyHandler.handler(this);
        }
        for (CBSFragmentLifeCyclerHandler destroyHandler : globalDestroyHandlers) {
            destroyHandler.handler(this);
        }
    }

    public void addActivityResultHandler(CBSActivityResultHandler handler) {
        if (!activityResultHandlers.contains(handler)) {
            activityResultHandlers.add(handler);
        }
    }

    public void removeActivityResultHandler(CBSActivityResultHandler handler) {
        if (activityResultHandlers.contains(handler)) {
            activityResultHandlers.remove(handler);
        }
    }

    public static void addGlobalActivityResultHandler(CBSActivityResultHandler handler) {
        if (!globalActivityResultHandlers.contains(handler)) {
            globalActivityResultHandlers.add(handler);
        }
    }

    public static void removeGlobalActivityResultHandler(CBSActivityResultHandler handler) {
        if (globalActivityResultHandlers.contains(handler)) {
            globalActivityResultHandlers.remove(handler);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        for (CBSActivityResultHandler activityResultHandler : activityResultHandlers) {
            activityResultHandler.onActivityResult(requestCode, resultCode, data);
        }
        for (CBSActivityResultHandler activityResultHandler : globalActivityResultHandlers) {
            activityResultHandler.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void doDelayed(DelayedTask delayedTask, Bundle data) {
        doDelayed(delayedTask, data, 0);
    }

    public void doDelayed(DelayedTask delayedTask, long delayedMS) {
        doDelayed(delayedTask, null, delayedMS);
    }

    public void doDelayed(DelayedTask delayedTask) {
        doDelayed(delayedTask, null, 0);
    }

    protected void hideSoftInput() {
        getCBSActivity().hideSoftInput();
    }

    protected void showSoftInput(View view) {
        getCBSActivity().showSoftInput(view);
    }
}
